//
//  XPGuidePageViewController.swift
//  PPKit
//
//  Created by xiaopin on 2020/12/2.
//  Copyright © 2020 scoresdata.net. All rights reserved.
//

import UIKit

/// 引导页
@objcMembers
class XPGuidePageViewController: UIViewController {
    
    var num = 3
    
    lazy var startButton: UIButton = {
        let button = UIButton.init(frame: pp_frame((pp_screen_width - 225)/2, pp_screen_height - 80 - 20 - pp_screen_tabbar_bottom_height, 222, 66))
        button.isHidden = true
        button.setImage("guide_start")
        button.addTarget_u(self, action: #selector(startAction(_:)))
        return button
    }()
    
    lazy var pageControl: UIPageControl = {
        let pageC = UIPageControl.init(frame: pp_frame(0, pp_screen_height - 30 - pp_screen_tabbar_bottom_height, pp_screen_width, 20))
        pageC.numberOfPages = num
        pageC.currentPage = 0
        pageC.pageIndicatorTintColor = pp_color(.CFFFFFF)
        pageC.currentPageIndicatorTintColor = pp_color(.C159F48)
        return pageC
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        let scrollView = UIScrollView.init(frame: pp_frame(0, 0, pp_screen_width, pp_screen_height))
        scrollView.backgroundColor = .white
        scrollView.bounces = false
        scrollView.isPagingEnabled = true
        scrollView.showsHorizontalScrollIndicator = false
        scrollView.delegate = self
        scrollView.contentSize = CGSize(width: pp_screen_width * num.f, height: pp_screen_height)
        scrollView.contentInsetAdjustmentBehavior = .never
        view.addSubview(scrollView)
        
        for i in 0..<num {
            let imageView = UIImageView.init(frame: pp_frame(pp_screen_width*i.f, 0, pp_screen_width, pp_screen_height))
            imageView.image = UIImage(named: "guide_\(i+1)")
            imageView.contentMode = .scaleAspectFill
            scrollView.addSubview(imageView)
        }
        
        view.addSubview(startButton)
        view.addSubview(pageControl)
    
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.setNavigationBarHidden(true, animated: true)
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        
        showPrivacyView()
    }
    
    func startAction(_ sender:UIButton){
        pp_keyWindow()?.rootViewController = XPTabBarController()
    }
    
    /// 显示隐私条款
    func showPrivacyView(){
        if !XPApplication.shared.isAgreedPrivacy {
            let rootVC = XPPrivacyViewController()
            let modalVC = XPNavigationController(rootViewController: rootVC)
            modalVC.modalPresentationStyle = .overFullScreen
            modalVC.modalTransitionStyle = .crossDissolve
            self.present(modalVC, animated: true)
        }
    }
}
extension XPGuidePageViewController: UIScrollViewDelegate {
    public func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        let page: Int = Int(scrollView.contentOffset.x / scrollView.bounds.size.width)
        startButton.isHidden = page != (num - 1)
        pageControl.currentPage = page
    }
}
